{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 1\n",
    "#### 1.1 \n",
    "Look up docs for the `convert` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "?convert"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 \n",
    "Assign `365` to a variable named `days`. Convert `days` to a float."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "days = 365\n",
    "convert(Float64, days)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.3 \n",
    "See what happens when you execute\n",
    "\n",
    "```julia\n",
    "convert(Int64, '1')\n",
    "```\n",
    "and\n",
    "\n",
    "```julia\n",
    "parse(Int64, '1')\n",
    "```\n",
    "\n",
    "What's the difference?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "convert(Int64, '1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parse(Int64, '1')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 2\n",
    "#### 2.1 \n",
    "Create a string that says \"hi\" 1000 times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"hi\"^1000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2\n",
    "Declare two variables\n",
    "\n",
    "```julia\n",
    "a = 3\n",
    "b = 4\n",
    "```\n",
    "and use them to create two strings:\n",
    "```julia\n",
    "\"3 + 4\"\n",
    "\"7\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3; b = 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"$a + $b\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"$(a + b)\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 3\n",
    "\n",
    "#### 3.1 \n",
    "Create an array, `a_ray`, with the following code:\n",
    "\n",
    "```julia\n",
    "a_ray = [1, 2, 3]\n",
    "```\n",
    "\n",
    "Add the number `4` to the end of this array and then remove it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_ray = [1, 2, 3]\n",
    "push!(a_ray, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2 \n",
    "Try to add \"Emergency\" as key to `myphonebook` with the value `string(911)` with the following code\n",
    "```julia\n",
    "myphonebook[\"Emergency\"] = 911\n",
    "```\n",
    "\n",
    "Why doesn't this work?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myphonebook = Dict(\"Jenny\" => \"867-5309\", \"Ghostbusters\" => \"555-2368\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myphonebook[\"Emergency\"] = 911\n",
    "#= \n",
    "\n",
    "Julia constructed `myphonebook` to match the arguments\n",
    "it was passed; both of which had `String`s for both their\n",
    "keys and values. We see that myphonebook is a\n",
    "`Dict{String,String} with 2 entries`. This means Julia\n",
    "will not accept integers as values in myphonebook.\n",
    "\n",
    "=#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This will work:\n",
    "myphonebook[\"Emergency\"] = \"911\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.3 \n",
    "Create a new dictionary called `flexible_phonebook` that has Jenny's number stored as a string and Ghostbusters' number stored as an integer. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flexible_phonebook = Dict(\"Jenny\" => \"867-5309\", \"Ghostbusters\" => 5552368)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.4\n",
    "Add the key \"Emergency\" with the value `911` (an integer) to `flexible_phonebook`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flexible_phonebook[\"Emergency\"] = 911"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.5\n",
    "Why can we add an integer as a value to `flexible_phonebook` but not `myphonebook`? How could we have initialized `myphonebook` so that it would accept integers or strings as values?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#= \n",
    "\n",
    "We constructed `flexible_phonebook` with two key-value\n",
    "pairs whose values were different types, so Julia needed\n",
    "to construct a dictionary that could hold `Any` value.\n",
    "Unlike myphonebook, flexible_phonebook is a \n",
    "`Dict{String,Any} with 2 entries`.\n",
    "\n",
    "To avoid this, we could have initialized myphonebook a\n",
    "an empty dictionary and added entries later. Or we\n",
    "could have explicitly told Julia that we wanted a\n",
    "dictionary that accepted objects of type `Any` as\n",
    "values. See examples!\n",
    "=#\n",
    "\n",
    "myphonebook = Dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Alternatively we could use a parametric type constructor to tell Julia explicitly\n",
    "# what types we want to populate our dictionary with.\n",
    "myphonebook = Dict{String, Any}(\"Jenny\" => \"867-5309\", \"Ghostbusters\" => \"555-2368\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Notebook 4\n",
    "\n",
    "#### 4.1 \n",
    "Write a loop that prints the squares of integers between 1 and 100."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in 1:100\n",
    "   println(i^2) \n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2\n",
    "\n",
    "Add to the code above a bit to create a dictionary, `squares` that holds integers and their squares as key, value pairs such that\n",
    "\n",
    "```julia\n",
    "squares[10] == 100\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "squares = Dict()\n",
    "for i in 1:100\n",
    "   squares[i] = i^2 \n",
    "end\n",
    "squares"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.3 \n",
    "Use an array comprehension to create an an array that stores the squares of all integers between 1 and 100."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[i^2 for i in 1:100]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 5\n",
    "\n",
    "#### 5.1 \n",
    "Write a conditional statement that prints a number if the number is even and the string \"odd\" if the number is odd."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 3\n",
    "if (n % 2) == 0\n",
    "    println(n)\n",
    "else\n",
    "    println(\"odd\")\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.2 \n",
    "Rewrite the code from 5.1 using a ternary operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "(n % 2) == 0 ? println(n) : println(\"odd\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 6\n",
    "\n",
    "#### 6.1 \n",
    "Write a function that adds 1 to its input."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x -> x + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "add1(x) = x + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.2 \n",
    "Use `map` or `broadcast` to increment every element of matrix `A` by `1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = rand(5,5)\n",
    "map(x -> x + 1, A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "broadcast(add1, A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.3 \n",
    "Use the broadcast dot syntax to increment every element of matrix `A` by `1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "add1.(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A .+ 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 7\n",
    "\n",
    "#### 7.1 \n",
    "Load the Primes package (source code at https://github.com/JuliaMath/Primes.jl)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# using Pkg; Pkg.add(\"Primes\")\n",
    "using Primes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7.2 \n",
    "Verify that you can now use the function `primes` to grab all prime numbers under 1,000,000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "primes(1000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 8\n",
    "\n",
    "#### 8.1 \n",
    "Given\n",
    "```julia\n",
    "x = -10:10\n",
    "```\n",
    "plot y vs. x for $y = x^2$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Plots; gr()\n",
    "x = -10:10\n",
    "y = x .^ 2\n",
    "plot(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(x -> x ^ 2, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.2 \n",
    "Execute the following code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = plot(x, x)\n",
    "p2 = plot(x, x.^2)\n",
    "p3 = plot(x, x.^3)\n",
    "p4 = plot(x, x.^4)\n",
    "plot(p1,p2,p3,p4,layout=(2,2),legend=false)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and then create a $4x1$ plot that uses `p1`, `p2`, `p3`, and `p4` as subplots."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = plot(x, x)\n",
    "p2 = plot(x, x.^2)\n",
    "p3 = plot(x, x.^3)\n",
    "p4 = plot(x, x.^4)\n",
    "plot(p1,p2,p3,p4,layout=(4,1),legend=false)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 9\n",
    "\n",
    "#### 9.1\n",
    "\n",
    "Extend the function `foo`, adding a method that takes only one input argument, which is of type `Bool`, and prints \"foo with one boolean!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(x::Bool) = println(\"foo with one boolean!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.2\n",
    "\n",
    "Check that the method being dispatched when you execute \n",
    "```julia\n",
    "foo(true)\n",
    "```\n",
    "is the one you wrote."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@which foo(true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 10\n",
    "\n",
    "#### 10.1\n",
    "Take the inner product (or \"dot\" product) of a vector `v` with itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = [1, 2, 3]\n",
    "v' * v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using LinearAlgebra\n",
    "dot(v, v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 10.2\n",
    "Take the outer product of a vector v with itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v * v'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 11\n",
    "\n",
    "#### 11.1\n",
    "\n",
    "What are the eigenvalues of matrix A?\n",
    "\n",
    "```\n",
    "A =\n",
    "[\n",
    " 140   97   74  168  131\n",
    "  97  106   89  131   36\n",
    "  74   89  152  144   71\n",
    " 168  131  144   54  142\n",
    " 131   36   71  142   36\n",
    "]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A =\n",
    "[\n",
    " 140   97   74  168  131\n",
    "  97  106   89  131   36\n",
    "  74   89  152  144   71\n",
    " 168  131  144   54  142\n",
    " 131   36   71  142   36\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eigdec = eigen(A)\n",
    "eigdec.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 11.2 \n",
    "Create a `Diagonal` matrix from the eigenvalues of `A`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Diagonal(eigdec.values)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 11.3 \n",
    "Create a `LowerTriangular` matrix from `A`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "LowerTriangular(A)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.0",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
